package a66weding.com.jiehuntong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;

import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.FriendsListBean;
import a66weding.com.jiehuntong.beans.StoreDetailGuess;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;

/**
 * Created by admin on 2016/8/5.
 * 商品列表适配器
 */
public class ProductDetailsAdapter extends BaseRecyclerAdapter<ProductDetailsAdapter.ViewHolder> {
    //如果有点击事件请设置一个监听;
    private OnItemClickLitener mOnItemClickLitener;

    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    private Context context;
    private List<StoreDetailGuess.DataBean> dataBeen;

    public ProductDetailsAdapter(Context context,List<StoreDetailGuess.DataBean> dataBeen){
        this.context = context;
       if (dataBeen!=null){
           this.dataBeen = dataBeen;
       }
    }
    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v = LayoutInflater.from(context).inflate(
                R.layout.item_store_content, parent, false);
        ViewHolder holder = new ViewHolder(v);
        return holder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position, boolean isItem) {
        if (mOnItemClickLitener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView,pos);
                    Log.e("点击的是哪个item",""+pos);
                }
            });
        }
        holder.item_store_content.setText(dataBeen.get(position).getNvc_Name());
        x.image().bind(holder.item_store_iv,dataBeen.get(position).getNvc_PicMain(), ImageOptionsUtil.getOptions316_720());
        holder.item_store_yuanjia.setVisibility(View.INVISIBLE);
        holder.item_store_discount_price.setText("¥ "+dataBeen.get(position).getDm_ShopPrice());
        holder.item_store_tv_num.setText(""+dataBeen.get(position).getI_Collections());
    }

    @Override
    public int getAdapterItemCount() {
        if (dataBeen==null){
            return 0;
        }
        return dataBeen.size();
    }
    public void append(List<StoreDetailGuess.DataBean> data) {
        if (this.dataBeen != null) {
            this.dataBeen.addAll(data);
        } else {
            this.dataBeen = data;
        }
        notifyDataSetChanged();
    }
    public class ViewHolder extends RecyclerView.ViewHolder{
        //图片;
        ImageView item_store_iv;
        //内容
        TextView item_store_content;
        //多少人收藏;
        TextView item_store_tv_num;
        //现价
        TextView item_store_discount_price;
        //原价
        TextView item_store_yuanjia;

        public ViewHolder(View itemView) {
            super(itemView);
/**
 *   hold.item_store_iv = (ImageView) convertView.findViewById(R.id.item_store_iv);
 hold.item_store_content = (TextView) convertView.findViewById(R.id.item_store_content);
 hold.item_store_tv_num = (TextView) convertView.findViewById(R.id.item_store_tv_num);
 hold.item_store_discount_price = (TextView) convertView.findViewById(R.id.item_store_discount_price);
 hold.item_store_yuanjia = (TextView) convertView.findViewById(R.id.item_store_yuanjia);
 */
            item_store_iv = (ImageView) itemView.findViewById(R.id.item_store_iv);
            item_store_content = (TextView) itemView.findViewById(R.id.item_store_content);
            item_store_tv_num= (TextView) itemView.findViewById(R.id.item_store_tv_num);
            item_store_discount_price = (TextView) itemView.findViewById(R.id.item_store_discount_price);
            item_store_yuanjia = (TextView) itemView.findViewById(R.id.item_store_yuanjia);
        }
    }

    public void clear() {
        if (dataBeen != null) {
            this.dataBeen.clear();
        }
    }

    public int getSize(){
        if (dataBeen==null){
            return 0;
        }
        return dataBeen.size();
    }
}
